﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessPlatformTool.BusinessObjectStructure;

namespace BusinessPlatformTool.DatabaseSQL
{
    internal static class CreateSQL
    {
        public static IList<string> GetQueryString(IBusinessObject BusObj)
        {
            IList<string> QueryList = new List<string>();
            string Query = string.Empty;
            #region 创建数据表
            foreach (var table in BusObj.Lines)
            {
                Query = "CREATE TABLE [dbo].[{0}] (\r\n";
                Query = string.Format(Query, table.Name);
                #region 字段
                foreach (var field in table.Lines)
                {
                    Query += "[{0}] [{1}]{2} {3},\r\n";
                    Query = string.Format(Query, field.Name,
                                                field.DbType,
                                                string.IsNullOrEmpty(field.PSize) ? "" : "(" + field.PSize + (string.IsNullOrEmpty(field.SSize) ? "" : "," + field.SSize) + ")",
                                                field.IsNull == true ? "NULL" : "NOT NULL");
                }
                #endregion
                #region 主键与联合主键
                IList<IField> PrimaryFields = table.Lines.GetPrimaryKey();
                IList<IField> UnionFields = table.Lines.GetUnionKey();
                if (PrimaryFields.Count > 0 || UnionFields.Count > 0)
                {
                    Query += "CONSTRAINT [PK_{0}] PRIMARY KEY CLUSTERED ({1}\r\n)\r\n";
                    var Key = string.Empty;
                    #region 主键
                    if (PrimaryFields.Count > 0)
                    {
                        foreach (var item in PrimaryFields)
                        {
                            Key += "\r\n[" + item.Name + "] ASC,";
                        }
                    }
                    #endregion
                    #region 联合主键
                    if (UnionFields.Count > 0)
                    {
                        foreach (var item in UnionFields)
                        {
                            Key += "\r\n[" + item.Name + "] ASC,";
                        }
                    }
                    #endregion
                    if (Key.Trim().EndsWith(","))
                        Key = Key.Trim().Substring(0, Key.Trim().Length - 1);
                    Query = string.Format(Query, table.Name, Key);
                    Query += "WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]";
                }
                #endregion
                if (Query.Trim().EndsWith(","))
                    Query = Query.Trim().Substring(0, Query.Trim().Length - 1);
                Query += ") ON [PRIMARY]";
                QueryList.Add(Query);
                #region 创建默认值
                Query = "ALTER TABLE [dbo].[{0}] ADD  CONSTRAINT [DF_{0}_{1}]  DEFAULT ({2}) FOR [{1}]";
                foreach (var field in table.Lines.GetDefaultValueFields())
                {
                    decimal DefaultValue;
                    if (decimal.TryParse(field.DefaultValue, out DefaultValue))
                        QueryList.Add(string.Format(Query, table.Name, field.Name, "(" + DefaultValue.ToString() + ")"));
                    else
                        QueryList.Add(string.Format(Query, table.Name, field.Name, "N'" + field.DefaultValue + "'"));
                }
                #endregion
            }
            #endregion
            #region 添加到对象
            Query = "INSERT INTO [BusinessObject] (ObjectCode,ObjectName,AutoKey,IsLog,LogInst) VALUES (N'{0}',N'{1}',{2},N'{3}',{4})";
            Query = string.Format(Query, BusObj.ObjectCode, BusObj.ObjectName, "1", BusObj.IsLog == true ? "Y" : "N", "null");
            QueryList.Add(Query);
            #endregion
            return QueryList;
        }
    }
}
